package springboot.springboot06_ssmp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import springboot.common_utils.R;
import springboot.springboot06_ssmp.entity.HomeNumber;
import springboot.springboot06_ssmp.mapper.HomeNumberMapper;
import springboot.springboot06_ssmp.service.IHomeNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author daiqiang
 * @since 2024-01-17
 */

@Slf4j
@CrossOrigin  //允许跨域
@RestController
@RequestMapping("homeNumber")
public class HomeNumberController {
    @Resource

    private IHomeNumberService homeNumberService;
    @Autowired
    private HomeNumberMapper homeNumberMapper;
    @GetMapping
    public R getAll(){
        return R.success(homeNumberService.list());
    }

    @PostMapping
    public R save(@RequestBody HomeNumber homeNumber){
        System.out.println(homeNumber.getAdmin());
        boolean a= Boolean.parseBoolean(homeNumber.getAdmin());
        if(a){
            homeNumber.setAdmin("Y");
        }else{   homeNumber.setAdmin("N");  }
        boolean flag=homeNumberService.save(homeNumber);
        if(flag) {
            return R.success("success");
        }else{
            return R.fail("fail");
        }
        //return  new  R (flag,flag?"添加成功^_^":"添加失败！");
    }

    @PutMapping
    public  R update(@RequestBody HomeNumber homeNumber){
        boolean a= Boolean.parseBoolean(homeNumber.getAdmin());
        if(a){
            homeNumber.setAdmin("Y");
        }else{   homeNumber.setAdmin("N");  }
        return  R.success(homeNumberService.updateById(homeNumber));
    }
    @DeleteMapping("{id}")
    public  R delete(@PathVariable Integer id){
        return  R.success(homeNumberService.removeById(id));
    }
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id) {

        return  R.success(homeNumberService.getById(id));
    }
    //分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public R getAllPage(@PathVariable int currentPage,@PathVariable int pageSize,HomeNumber homeNumber){
        System.out.println("参数----》"+homeNumber);
        IPage<HomeNumber> pageHomeNumber=homeNumberService.getPage(currentPage,pageSize,homeNumber );

        return R.success(pageHomeNumber);

    }

    @GetMapping("/list")
    public R<Map<String,Object> > getUserList(@RequestParam(value="name",required=false) String  name,
                                              @RequestParam (value="card",required=false) String  card,
                                              @RequestParam (value="pageNo",required=false) Long  pageNo,
                                              @RequestParam (value="pageSize",required=false) Long  pageSize
                                              ){
        LambdaQueryWrapper<HomeNumber> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasLength(name),HomeNumber::getName,name);
        lqw.eq(StringUtils.hasLength(card),HomeNumber::getCard,card);

        Page<HomeNumber> page = new Page<>(pageNo,pageSize);
        homeNumberMapper.selectPage(page,lqw);

        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());
        return R.success(data);

    }



}
